package com.lht.testDemo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-11-15
 */
public class TreeToList {

    public static void main(String[] args) {

        Tree S = new Tree("S", null);
        Tree X = new Tree("X", null);
        List<Tree> second = new ArrayList<>();
        second.add(S);
        second.add(X);

        Tree A = new Tree("A",second);
        Tree B = new Tree("B", null);
        List<Tree> first = new ArrayList<>();
        first.add(A);
        first.add(B);

        Tree root = new Tree("name", first);

        List<Tree> trees = new ArrayList<>();
        trees.add(root);
        List<Tree> list = new ArrayList<>();
        while (trees != null && !trees.isEmpty()){
            List<Tree> next = new ArrayList<>();
            for (Tree tree : trees) {
                if (tree != null) {
                    list.add(tree);
                    if (tree.getChilds() != null) {
                        next.addAll(tree.getChilds());
                    }
                }
            }
            trees = next;
        }

        list.forEach(item->{
            System.out.println(item.getName());
        });
    }
}

class Tree{

    private String name;

    private List<Tree> childs;

    public Tree(String name) {
        this.name = name;
    }

    public Tree(String name, List<Tree> parent) {
        this.name = name;
        this.childs = parent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Tree> getChilds() {
        return childs;
    }

    public void setChilds(List<Tree> childs) {
        this.childs = childs;
    }
}
